Terminal.applyAddon(fit);

const [auth, addr] = prompt("ssh", "root:besovideo@192.168.88.25:22").split("@");
const [user, password] = auth.split(":");

let cols_rows = get_term_size();

const term = new Terminal({
    cols: cols_rows.cols,
    rows: cols_rows.rows,
});
term.open(document.getElementById("main"));

const ws = new WebSocket(`ws://${location.host}/api/ssh?addr=${addr}`);
ws.onopen = () => {
    ws.send(JSON.stringify({type: "login", data: utoa(user)}));
    ws.send(JSON.stringify({type: "password", data: utoa(password)}));

    term.on("data", data => {
        const msg = {type: "stdin", data: btoa(data)};
        ws.send(JSON.stringify(msg));
    });

    term.on("resize", e => {
        const msg = {type: "resize", ...e};
        ws.send(JSON.stringify(msg));
    });

    term.fit();
    window.addEventListener("resize", () => term.fit());
};

ws.onmessage = e => {
    const msg = JSON.parse(e.data);
    switch (msg.type) {
        case "stdout":
        case "stderr":
            term.write(atou(msg.data));
    }
};
ws.onerror = console.error;

function atou(encodeString) {
    return decodeURIComponent(escape(atob(encodeString)));
}

function utoa(rawString) {
    return btoa(encodeURIComponent(rawString));
}


function get_term_size() {
    let init_width = 9;
    let init_height = 17;
    let windows_width = window.innerWidth;
    let windows_height = window.innerHeight;
    return {
        cols: Math.floor(windows_width / init_width),
        rows: Math.floor(windows_height / init_height),
    }
}